#include "skmodelprimitive.h"


SkModelPrimitive::SkModelPrimitive(SketchModelWrapper* modelo,QObject *parent) :
    QAbstractTableModel(parent)
{
    model = modelo;
    map = new std::map<int,std::string>(model->getPrimitiveData());
}
int SkModelPrimitive::rowCount( const QModelIndex & parent  ) const {
	Q_UNUSED(parent);
	return map->size();
}
int SkModelPrimitive::columnCount ( const QModelIndex & parent ) const{
	Q_UNUSED(parent);
    return 2;
}
QVariant SkModelPrimitive::data ( const QModelIndex & index, int role ) const{
     if (!index.isValid())
         return QVariant();
     if(role==Qt::DisplayRole || role==Qt::EditRole){

         if(index.column()==0){
        	 int i=0;
        	 for(std::map<int,std::string>::iterator it = map->begin(); it!=map->end(); it++){
        		 if(index.row()==i){
        			 return it->first;
        		 }
        		 i++;
        	 }

         }else if(index.column()==1){
        	 int i=0;
        	 for(std::map<int,std::string>::iterator it = map->begin(); it!=map->end(); it++){
        		 if(index.row()==i){
        			 return QString(it->second.c_str());
        		 }
        		 i++;
        	 }
         }

     }

    return QVariant();

}
bool SkModelPrimitive::setData ( const QModelIndex & index, const QVariant & value, int role  ) {
	Q_UNUSED(index);
	Q_UNUSED(value);
	Q_UNUSED(role);
	return false;

}

QVariant SkModelPrimitive::headerData(int section, Qt::Orientation orientation,int role ) const{

    if (role != Qt::DisplayRole) return QVariant();

    if (orientation == Qt::Horizontal){

        switch(section){
            case 0:
                return QString("id");
            case 1:
                 return QString("type");
        }

    }

    return QString("%1").arg(section);


}

Qt::ItemFlags SkModelPrimitive::flags(const QModelIndex &index) const{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    switch(index.column()){
        case 0:
            return QAbstractItemModel::flags(index);
        case 1:
            return QAbstractItemModel::flags(index) ;

    }

    return NULL;
}
void SkModelPrimitive::resetList(){
	delete map;

	map = new std::map<int,std::string>(model->getPrimitiveData());
	reset();
}

SkModelPrimitive::~SkModelPrimitive(){
	delete map;
}
